<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Basic usage</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="shmop.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.shmop.html">Shared Memory 函数</a></div>
 <div class="up"><a href="shmop.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="shmop.examples-basic" class="section">
  <h2 class="title">Basic usage</h2>
  <p class="para">
   <div class="example" id="example-3629">
    <p><strong>Example #1 Shared Memory Operations Overview</strong></p> 
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;100&nbsp;byte&nbsp;shared&nbsp;memory&nbsp;block&nbsp;with&nbsp;system&nbsp;id&nbsp;of&nbsp;0xff3<br /></span><span style="color: #0000BB">$shm_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">shmop_open</span><span style="color: #007700">(</span><span style="color: #0000BB">0xff3</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0644</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$shm_id</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Couldn't&nbsp;create&nbsp;shared&nbsp;memory&nbsp;segment\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Get&nbsp;shared&nbsp;memory&nbsp;block's&nbsp;size<br /></span><span style="color: #0000BB">$shm_size&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">shmop_size</span><span style="color: #007700">(</span><span style="color: #0000BB">$shm_id</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"SHM&nbsp;Block&nbsp;Size:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$shm_size&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&nbsp;has&nbsp;been&nbsp;created.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Lets&nbsp;write&nbsp;a&nbsp;test&nbsp;string&nbsp;into&nbsp;shared&nbsp;memory<br /></span><span style="color: #0000BB">$shm_bytes_written&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">shmop_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$shm_id</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"my&nbsp;shared&nbsp;memory&nbsp;block"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$shm_bytes_written&nbsp;</span><span style="color: #007700">!=&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #DD0000">"my&nbsp;shared&nbsp;memory&nbsp;block"</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Couldn't&nbsp;write&nbsp;the&nbsp;entire&nbsp;length&nbsp;of&nbsp;data\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Now&nbsp;lets&nbsp;read&nbsp;the&nbsp;string&nbsp;back<br /></span><span style="color: #0000BB">$my_string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">shmop_read</span><span style="color: #007700">(</span><span style="color: #0000BB">$shm_id</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$shm_size</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$my_string</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Couldn't&nbsp;read&nbsp;from&nbsp;shared&nbsp;memory&nbsp;block\n"</span><span style="color: #007700">;<br />}<br />echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;data&nbsp;inside&nbsp;shared&nbsp;memory&nbsp;was:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$my_string&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//Now&nbsp;lets&nbsp;delete&nbsp;the&nbsp;block&nbsp;and&nbsp;close&nbsp;the&nbsp;shared&nbsp;memory&nbsp;segment<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">shmop_delete</span><span style="color: #007700">(</span><span style="color: #0000BB">$shm_id</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Couldn't&nbsp;mark&nbsp;shared&nbsp;memory&nbsp;block&nbsp;for&nbsp;deletion."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">shmop_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$shm_id</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="shmop.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.shmop.html">Shared Memory 函数</a></div>
 <div class="up"><a href="shmop.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
